﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

namespace TRPOP.Common
{
    /// <summary>
    /// EditorImgHelper
    /// </summary>
    public class EditorImgHelper
    {
        //strFilePath = CKFinder.Settings.ConfigFile.Current.BaseUrl.Replace("~/", "/");
        /// <summary>
        /// EditorUploadFile
        /// </summary>
        /// <param name="strUploadFileName">strUploadFileName</param>
        /// <param name="strUploadFileWithPath">strUploadFileWithPath</param>
        /// <param name="strFilePath">strFilePath</param>
        /// <param name="intSYSMaxWidth">intSYSMaxWidth</param> 
        /// <returns></returns>
        public static AttachmentInfo EditorUploadFile(ref string strUploadFileName, ref string strUploadFileWithPath, string strFilePath,int intSYSMaxWidth)
        {

            #region 基本信息读取，读取 水印信息 文件长高度限制 等比压缩 水印位置 等信息

            //是否加水印 水印位置
            bool blnResizePicWater = CookieHelper.GetCookie("chkPicWater").ToLower() == "true";
            int intWaterPicLoaction = CookieHelper.GetCookie("chkPicWaterSelect").ToInt();  

            //水印图片
            string strWaterPic = CookieHelper.GetCookie("PicWaterImg").ToString().UrlDecode();
            if (FileUtil.IsExist(Util.GetMapPath(strWaterPic)) == false)
            {
                strWaterPic = string.Empty;
            }

            //是否等比缩放
            bool blnPicResize = CookieHelper.GetCookie("chkPicResize").ToLower() == "true";

            //宽高限制
            int intMaxWidth = CookieHelper.GetCookie("PicResizeWidth").ToString().ToInt();
            int intMaxHeight = CookieHelper.GetCookie("PicResizeHeight").ToString().ToInt();
            if (intMaxWidth <= 0)
            {
                intMaxWidth = intSYSMaxWidth;
            }
            if (intMaxHeight <= 0)
            {
                intMaxHeight = int.MaxValue;
            }

            //水印位置
            CMSImageHelper.WaterPicLocation enumLoaction = CMSImageHelper.WaterPicLocation.BottomCenter;
            if (intWaterPicLoaction >= 1 && intWaterPicLoaction <= 5)
            {
                enumLoaction = (CMSImageHelper.WaterPicLocation)Enum.ToObject(typeof(CMSImageHelper.WaterPicLocation), intWaterPicLoaction);
            }
            else
            {
                strWaterPic = string.Empty;
            }

            //文件名增加的部分，例如 _100×100
            string strAddFileName = "";
            if (blnPicResize == false && intMaxWidth != int.MaxValue && intMaxHeight != int.MaxValue)//如果强制压缩，则加个后缀名
                strAddFileName = "_" + intMaxWidth + "×" + intMaxHeight;

            //将所有上传的文件保存至session里，在保存信息时将这些图片添加至图片集合
            string strUpfiles = string.Empty;
            if (HttpContext.Current.Session[PageAssist.EditorUpLoadFieldKey] != null)
            {
                strUpfiles = HttpContext.Current.Session[PageAssist.EditorUpLoadFieldKey].ToString();
            }

            strFilePath = strFilePath.Replace("~/", "/").Replace("//", "/");
            #endregion

            #region 根据获取的 宽高度限制 等比压缩 水印 水印位置 进行不同的图片处理

            AttachmentInfo oAttachmentInfo = null;

            if (blnPicResize == false && intMaxWidth != int.MaxValue && intMaxHeight != int.MaxValue)//强制压缩,即不等比缩放
            {
                oAttachmentInfo = CMSImageHelper.CMSImageUpLoad(0, strFilePath, strWaterPic, "jpg|jpeg|gif|png", intMaxWidth, intMaxHeight, enumLoaction);
            }
            else if (blnResizePicWater)////如果需要加水印
            {
                oAttachmentInfo = CMSImageHelper.CMSImageUpLoad(0, strFilePath, strWaterPic, "jpg|jpeg|gif|png", enumLoaction);
                if (oAttachmentInfo.ErrorDescription.IsEmpty())
                {
                    string strPhysicPath = RequestHelper.GetMapPath(oAttachmentInfo.FileName);
                    if (FileUtil.IsExist(strPhysicPath))
                    {
                        ImageTools.ResizeImage(strPhysicPath, strPhysicPath, intMaxWidth, intMaxHeight, true, 80);
                    }
                }
            }
            else //不需要水印，则等比缩放图片
            {
                //原图上传,并压缩为指定的宽和高
                oAttachmentInfo = CMSImageHelper.CMSImageUpLoadAutomini(0, strFilePath, string.Empty, "jpg|jpeg|gif|png", intMaxWidth, enumLoaction);
            }

            #endregion

            string strFileName = string.Empty;
            if (oAttachmentInfo.ErrorDescription.IsEmpty())
            {

                strFileName = oAttachmentInfo.FileName.Replace(strFilePath, "").Trim();
                while (strFileName.StartsWith("/") && strFileName.Length > 1)
                {
                    strFileName = strFileName.Substring(1);
                }

                //将所有上传的文件增加值session
                strUpfiles = strUpfiles + (string.IsNullOrEmpty(strUpfiles) ? "" : "|") + (strFilePath + "/" + strFileName).Replace("//", "/");
                if (string.IsNullOrEmpty(strAddFileName) == false)
                {
                    strUpfiles = strUpfiles + (string.IsNullOrEmpty(strUpfiles) ? "" : "|") + (strFilePath + "/" + (strFileName.Insert(strFileName.IndexOf("."), strAddFileName))).Replace("//", "/");
                }
                HttpContext.Current.Session[PageAssist.EditorUpLoadFieldKey] = strUpfiles;

                strUploadFileName = strFileName.Insert(strFileName.IndexOf("."), strAddFileName);
                strUploadFileWithPath = (strFilePath + "/" + (strFileName.Insert(strFileName.IndexOf("."), strAddFileName))).Replace("//", "/");//oAttachmentInfo.FileName;
            }
            else
            {
                strUploadFileName = string.Empty;
                strUploadFileWithPath = string.Empty;
            }

            return oAttachmentInfo;
        }
    }
}